package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrdersettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import static com.itheima.utils.DateUtils.parseString2Date;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrdersettingDao ordersettingDao;
    @Autowired
    private MemberDao memberDao;


    @Override
    public Result order(Map map) throws Exception {
        //1.判断预约的日期是否可以预约
        String orderDate = (String) map.get("orderDate");
        System.out.println("orderDate =------------------------ " + orderDate);
        //根据ordersetting的日期查询看是否有当天的预约
        OrderSetting orderSetting = ordersettingDao.findByOrderDate(parseString2Date(orderDate));
        System.out.println("orderSetting =0000000000000000000000 " + orderSetting);
        if (orderSetting == null) {
            //当天不能进行预约体检
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //2.查询日期是否已经预约满
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (reservations > number) {
            //预约已经满了
            return new Result(false, MessageConstant.ORDER_FULL);
        }

        //3.判断用户是否同一时间多次预约
        String telephone = (String) map.get("telephone");
        //根据用户手机号查询用户
        Member member = memberDao.findByTelephone(telephone);
        System.out.println("member =111111111111111111111111111111 " + member);
        if (member != null) {
            //用户手机号判断是否是同一个时间同一个套餐
            Integer memberId = member.getId();
            String setmealId = (String) map.get("setmealId");
            String orderDate1 = (String) map.get("orderDate");
            //创建会员对象并设置数据
            Order order = new Order();
            order.setSetmealId(Integer.parseInt(setmealId));
            order.setId(memberId);
            order.setOrderDate(DateUtils.parseString2Date(orderDate1));
            //根据条件进行查询
            List<Order> list = orderDao.findByCondition(order);

            System.out.println("list =222222222222222222222222 " + list);
            //说明是重复预约
                if (list != null && list.size() > 0) {
                    //说明用户在重复预约，无法完成再次预约
                    return new Result(false, MessageConstant.HAS_ORDERED);
                }
            }else {
            //4.自动完成会员注册
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            //自动完成会员注册
            memberDao.add(member);

        }
        //5预约成功,更新数据
            Order order = new Order();
        //设置会员id
        order.setMemberId(member.getId());
        //预约日期
        order.setOrderDate(DateUtils.parseString2Date(orderDate));
        //设置预约类型
        order.setOrderType((String) map.get("orderType"));
        //到诊状态
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        //套餐ID
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));
        //插入数据到t_order表中,预约订单信息表
        orderDao.add(order);

        //设置预约人数加1
        orderSetting.setReservations(  orderSetting.getReservations() + 1);
        // 更新到数据库
        ordersettingDao.editReservationsByOrderDate(orderSetting);

        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());


    }
}
